/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlers;

import Pojos.SanPham;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author ToanNhan
 */
@WebServlet(name = "KetQuaTimKiemController", urlPatterns = {"/KetQuaTimKiemController"})
public class KetQuaTimKiemController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            String command = request.getParameter("command");
            if (command == null) {
                response.getOutputStream().close();
            } else {
                command = command.toLowerCase();
                if (command.compareTo("layketqua") == 0) {
                    layKetQua(request, response);
                }
            }
        } finally {
        }
    }

    protected void layKetQua(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tuKhoa = request.getParameter("tuKhoa");
        String tieuChi = request.getParameter("tieuChi");
        String tuGia = request.getParameter("tuGia");
        String denGia = request.getParameter("denGia");

        tuKhoa = "%" + tuKhoa + "%";
        String strQuery = "From SanPham as sp Where ";
        if (tieuChi.compareTo("danhMuc") == 0) {
            strQuery += "sp.danhMucSanPham.tenDanhMuc like '";
        } else {
            if (tieuChi.compareTo("hangSanXuat") == 0) {
                strQuery += "sp.hangSanXuat.tenHangSanXuat like '";
            } else {
                strQuery += "sp.tenSanPham like '";
            }
        }
        strQuery += tuKhoa + "'";
        int soTien = 0;
        try {
            soTien = Integer.parseInt(tuGia);
        } catch (Exception ex) {
            System.out.println("KetQuaTimKiemController : LayKetQua : " + ex.getMessage());
        }
        strQuery += " and sp.giaBan > " + soTien;

        try {
            soTien = Integer.parseInt(denGia);
        } catch (Exception ex) {
            System.out.println("KetQuaTimKiemController : LayKetQua : " + ex.getMessage());
        }
        if (soTien != -1) {
            strQuery += " and sp.giaBan < " + soTien;
        }

        SessionFactory factory = new Configuration().configure("Pojos/hibernate.cfg.xml").buildSessionFactory();
        Session session = factory.openSession();
        Query query = session.createQuery(strQuery);
        try {
            List<SanPham> lstSanPham = query.list();
            request.setAttribute("ketQua", lstSanPham);
            request.getRequestDispatcher("KetQuaTimKiem.jsp").forward(request, response);
            session.close();
            factory.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            session.close();
            factory.close();
        }


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
